package org.stormphoenix.bbsfamily.entity

import org.hibernate.validator.constraints.Length
import java.io.Serializable
import java.sql.Timestamp
import javax.persistence.*
import javax.validation.constraints.Email
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotNull

@Entity
@Table(name = "user")
data class UserDO(

    @field:NotBlank(message = "用户名不能为空")
    @field:Length(min = 1, max = 15)
    var username: String = "",

    @field:NotBlank(message = "密码不能为空")
    @field:Length(min = 5)
    var password: String = "",

    @field:Length(min = 0, max = 15)
    var nickname: String = ""
) : Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long = -1

    @field:Email
    var email: String = ""

    var enabled: Boolean = true

    @field:NotNull
    var regTime: Timestamp? = null

    //    TODO list 修改成 set
    @ManyToMany(cascade = [CascadeType.MERGE])
    @JoinTable(
        name = "user_role", joinColumns = [JoinColumn(name = "user_id", referencedColumnName = "id")],
        inverseJoinColumns = [JoinColumn(name = "role_id", referencedColumnName = "id")]
    )
    var roles: List<RoleDO>? = mutableListOf()
}
